package walletadminlogic

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/app/rpc/model/pg/wallet"
	pbuser "navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/userkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
)

type PushUserWithdrawLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPushUserWithdrawLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PushUserWithdrawLogic {
	return &PushUserWithdrawLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 提现发货
func (l *PushUserWithdrawLogic) PushUserWithdraw(in *pb.PushUserWithdrawReq) (*pb.PushUserWithdrawResp, error) {
	defer utils.RecoverPanic()

	l.Logger.Infof("PushUserWithdraw. in: %+v", in)
	var wf *wallet.UserWithdrawFlow
	var err error
	wf, err = l.svcCtx.UserWithdrawFlowModel.FindOne(l.ctx, in.FlowNo)
	if err != nil {
		if err == wallet.ErrNotFound {
			err = xerr.NewGrpcErrCodeMsg(xerr.WalletFlowNotFound, err.Error())
		}
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	// 修改提现流水的发货备注
	wf.PushMsg = in.PushMsg
	wf.State = walletkey.WithdrawFlowPushed
	err = l.svcCtx.UserWithdrawFlowModel.UpdatePush(l.ctx, wf.FlowNo, wf.State, wf.PushMsg)
	if err != nil {
		l.Logger.Errorf("PushUserWithdraw Update. err: %+v", err)
		return nil, err
	}
	msgContent := userkey.UserNotifyMsgContent1{
		Text:     in.PushMsg,
		Url:      "",
		PagePath: "",
	}
	var cBuf []byte
	cBuf, err = json.Marshal(msgContent)
	if err != nil {
		l.Logger.Errorf("UpdateDepositResultLogic after json.Marshal(msgContent) err:%v", err)
		return nil, xerr.NewGrpcErrCodeMsg(xerr.ServerCommonError, err.Error())
	}
	l.Logger.Infof("PushUserWithdraw. wf: %+v", wf)
	req := &pbuser.AddUserNotifyMsgReq{
		Uid:        wf.Uid,
		Title:      userkey.UserNotifyMsgTitleWithdrawAuditPass04EnIn,
		MsgType:    userkey.UserNotifyMsgTypeWithdrawAuditPass04,
		MsgContent: string(cBuf),
	}
	msg, err := l.AddUserNotifyMsg(req)
	if err != nil {
		l.Logger.Errorf("PushUserWithdraw err : %+v , msg : %+v", err, msg)
		return nil, err
	}
	return &pb.PushUserWithdrawResp{}, nil
}

// 新增通知消息
func (l *PushUserWithdrawLogic) AddUserNotifyMsg(in *pbuser.AddUserNotifyMsgReq) (*pbuser.AddUserNotifyMsgResp, error) {
	err := l.svcCtx.UserAccountModel.Trans(l.ctx, func(ctx context.Context, tx sqlx.Session) error {
		unm := usercenter.UserNotifyMsg{
			Uid:        in.Uid,
			MsgType:    in.MsgType,
			MsgContent: in.MsgContent,
			IsRead:     dbkey.Enable,
			Title:      in.Title,
		}
		l.Logger.Infof("AddUserNotifyMsg. in: %+v", in)
		// 增加消息
		_, err2 := l.svcCtx.UserNotifyMsgModel.InsertTrans(ctx, tx, &unm)
		if err2 != nil {
			l.Logger.Errorf("err == ", zap.Error(err2))
			return xerr.NewGrpcErrCodeMsg(xerr.DbError, err2.Error())
		}
		// 更新消息条数
		err2 = l.svcCtx.UserNotifyMsgStatModel.AddMsgCount(ctx, tx, in.Uid, 1)
		if err2 != nil {
			l.Logger.Errorf("err == ", zap.Error(err2))
			return xerr.NewGrpcErrCodeMsg(xerr.DbError, err2.Error())
		}
		return nil
	})
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}
	return &pbuser.AddUserNotifyMsgResp{}, nil
}
